package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import cn.hutool.core.util.StrUtil;
import io.renren.dao.WxuserDao;
import io.renren.dto.WxuserDTO;
import io.renren.entity.WxuserEntity;
import io.renren.service.WxuserService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * ${comments}
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-05-20
 */
@Service
public class WxuserServiceImpl extends CrudServiceImpl<WxuserDao, WxuserEntity, WxuserDTO> implements WxuserService {

    @Override
    public QueryWrapper<WxuserEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<WxuserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public WxuserDTO getByWxuseroid(String wxuseroid) {
        return baseDao.getByWxuseroid(wxuseroid);
    }

    @Override
    public WxuserEntity findOneByOpenId(String openId) {
        QueryWrapper<WxuserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("MiniOpenID", openId);
        return baseDao.selectOne(wrapper);
    }

    @Override
    public void updateTradermFk(WxuserEntity wxUserInfo) {
        wxUserInfo.setTradermFk(null);
        baseDao.update(wxUserInfo, new QueryWrapper<WxuserEntity>().eq("WXUserOID", wxUserInfo.getWxuseroid()));
    }

    @Override
    public void updateFkAndRemarkByWxUserId(String tradermFk, String mobile, String wxUserFk) {
        WxuserEntity wxuserEntity = new WxuserEntity();
        wxuserEntity.setTradermFk(tradermFk);
        wxuserEntity.setWxremark(mobile);

        QueryWrapper<WxuserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("WXUserOID", wxUserFk);
        baseDao.update(wxuserEntity, wrapper);
    }

    @Override
    public WxuserEntity findOneByUnionid(String unionId) {
        QueryWrapper<WxuserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("Unionid", unionId);
        return baseDao.selectOne(wrapper);
    }

    @Override
    public void updateWxOpenIdByUnionId(WxuserEntity wxuserEntity) {
        WxuserEntity wxuser = new WxuserEntity();
        wxuser.setWxopenid(wxuserEntity.getWxopenid());
        baseDao.update(wxuser, new QueryWrapper<WxuserEntity>().eq("WXUserOID", wxuserEntity.getWxuseroid()));
    }

    @Override
    public WxuserEntity findOneByMiniOpenId(String openId) {
        QueryWrapper<WxuserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("MiniOpenID", openId);
        return baseDao.selectOne(wrapper);
    }

    @Override
    public void updateUnionIdByMiniOpenId(WxuserEntity wxuserEntity) {
        WxuserEntity wxuser = new WxuserEntity();
        wxuser.setUnionid(wxuserEntity.getUnionid());
        baseDao.update(wxuser, new QueryWrapper<WxuserEntity>().eq("MiniOpenID", wxuserEntity.getMiniopenid()));
    }

    @Override
    public WxuserEntity findOneByWxOpenid(String openId) {
        QueryWrapper<WxuserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("WXOpenID", openId);
        return baseDao.selectOne(wrapper);
    }

    @Override
    public void updateUnionIdByWxOpenId(WxuserEntity wxuserEntity2) {
        WxuserEntity wxuser = new WxuserEntity();
        wxuser.setUnionid(wxuserEntity2.getUnionid());
        baseDao.update(wxuser, new QueryWrapper<WxuserEntity>().eq("WXUserOID", wxuserEntity2.getWxuseroid()));
    }

    @Override
    public WxuserEntity findOneByWXUserOid(String userId) {
        QueryWrapper<WxuserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("WXUserOID", userId);
        return baseDao.selectOne(wrapper);
    }

    @Override
    public void setWxOpenIdIsNull(String wxuseroid) {
        baseDao.setWxOpenIdIsNull(wxuseroid);
    }

    @Override
    public WxuserEntity findOneByTraderMOID(String traderMOID) {
        return baseDao.selectOne(new QueryWrapper<WxuserEntity>().eq("TraderM_FK", traderMOID));
    }

    @Override
    public void setTraderMFKIsNull(String wxuseroid) {
        baseDao.setTraderMFKIsNull(wxuseroid);
    }
}